package auctionServer.utilities;

import java.util.Calendar;
import java.util.Date;
import java.util.Stack;

public class Auction {

	private static int counter=0; 
	private String description;
	private String owner;
	private Calendar from;
	private Calendar to;
	private int id;
	private int currentPrice;
	private boolean notificationSent;
	private boolean ownerNotification;
	private Bidder justLost;
	
	private Stack<Bidder> bidders;
		
	public Auction(int duration, String description, String owner){
		this.owner=owner;
		
		this.description=description;
		
		this.id=counter;
		counter++;
		
		this.from=Calendar.getInstance();
		
		this.to=this.from;
		this.to.add(Calendar.SECOND, duration);
		
		this.currentPrice=0;
		
		this.notificationSent=false;
		
		this.bidders=new Stack<Bidder>();
		this.ownerNotification=false;
		this.justLost=null;
	}
	
	public synchronized boolean bid(String user,int amount){
		
		if(bidders.isEmpty()){
			bidders.add(new Bidder(user, Calendar.getInstance(), amount));
			currentPrice=amount;
			return true;
		}
		else if(bidders.peek().getAmount()<amount){
			justLost=bidders.peek();
			bidders.add(new Bidder(user, Calendar.getInstance(), amount));
			currentPrice=amount;
			return true;
		}else{
			return false;
		}
	}
	public synchronized boolean signedBid(String user,int amount,Calendar timestamp){
		if(bidders.isEmpty()){
			bidders.add(new Bidder(user, timestamp, amount));
			currentPrice=amount;
			return true;
		}else{
			if(bidders.peek().getBiddingTime().before(timestamp)&&(bidders.peek().getAmount()<amount)){
				justLost=bidders.peek();
				bidders.add(new Bidder(user, timestamp, amount));
				currentPrice=amount;
				return true;
			}else if(bidders.peek().getBiddingTime().after(timestamp)&&(bidders.peek().getAmount()<amount)){
				bidders.pop();
				bidders.add(new Bidder(user, timestamp, amount));
				currentPrice=amount;
				return true;
			}else if(bidders.peek().getBiddingTime().after(timestamp)&&(bidders.peek().getAmount()>amount)){
				Bidder temp=bidders.pop();
				justLost=new Bidder(user, timestamp, amount);
				bidders.add(new Bidder(user, timestamp, amount));
				bidders.add(temp);
				currentPrice=amount;
				return true;
			}else{
				return false;
			}
		}

	}
	public synchronized boolean expired(){
		if(Calendar.getInstance().before(to)){
			return false;
		}else{
			return true;
		}
	}
	public synchronized Calendar getFrom(){
		return from;
	}
	
	public synchronized Calendar getTo(){

		return to;
	}
	
	public synchronized int getCurrentPrice(){
		
		return currentPrice;
	}
	
	public synchronized int getId(){
		return id;
	}
	
	public synchronized String getOwner(){
		return owner;
	}
	
	public synchronized Bidder getJustLost(){
		return justLost;
	}
	
	public synchronized String getDescription(){
		return description;
	}
	
	public synchronized Bidder peekBidders(){
		return bidders.peek();
	}

	public synchronized String getWinningBidder(){
		if(bidders.empty()!=true){
			return bidders.peek().getName();
		}else{
			return "none";
		}
	}
	public synchronized boolean getNotificationSent(){
		return notificationSent;
	}
	public synchronized void setNotificationSent(boolean sent){
		notificationSent=sent;
	}
	public synchronized boolean getOwnerNotification(){
		return ownerNotification;
	}
	public synchronized void setOwnerNotification(boolean sent){
		ownerNotification=sent;
	}

}